import { search, get } from "../../utils/db"
Page({
  data: {
    lists: []
  },
  async onLoad(e) {
    var keyword = e.keyword;
    let _id = e.id
    if (keyword) {
      var result = await search('menu', keyword);
      var lists = result.data;
    } else if (_id) {
      var result = await get('menu', { classId: _id });
      this.setData({
        lists: result.data
      })
    }
    //在遍历对象之前先进行判断，判断对象是否为空，然后再去遍历该对象，就会成功避免这个问题。
    if(!lists){
      return
    }
    lists.forEach(item => {
      if (item.views == 0) {
        item.star = 0;
      } else if (item.views > 0 && item.views <= 10) {
        item.star = 1;
      } else if (item.views > 10 && item.views <= 20) {
        item.star = 2;
      } else if (item.views > 20 && item.views <= 30) {
        item.star = 3;
      } else if (item.views > 30 && item.views <= 40) {
        item.star = 4;
      } else {
        item.star = 5
      }
    })
    this.setData({
      lists,
    })
  },
  toDetail(e) {
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.id,
    })
  },


})